/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年12月8日
 * @version 0.1
 */
package com.suredy.app.detection.ctrl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.Constants;
import com.suredy.app.detection.entity.SmellandTasteEntity;
import com.suredy.app.detection.entity.SmellandTasteItemEntity;
import com.suredy.app.detection.service.SmellandTasteSrv;
import com.suredy.core.ctrl.BaseCtrl;
import com.suredy.core.mvc.model.MessageModel;


/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value = "/app")
public class SmellandTasteCtrl extends BaseCtrl{

	private final static Logger log = LoggerFactory.getLogger(SmellandTasteCtrl.class);

	@Autowired
	private SmellandTasteSrv service;


	@RequestMapping(value = "detection/smellandtaste-detail/{id}")
	public ModelAndView eidt(@PathVariable String id) {
		ModelAndView view = new ModelAndView("/app/detection/smellandtaste-detail");
		if (StringUtils.isBlank(id)) {
			log.info("Parameter String[taskId] is blank.");
			return view;
		}
		SmellandTasteEntity vo = this.service.getById(id);
		if (vo == null) {
			log.info("Can't found the task information which ID is '" + id + "'");
			return view;
		}
		view.addObject("smellandtaste", vo);
		view.addObject("taskId", id);
		return view;
	}

	@RequestMapping(value = "detection/smellandtaste-edit", method=RequestMethod.POST)
	@ResponseBody
	public Object doEdit(HttpServletRequest request) {
		String id = request.getParameter("id");
		String sampleDate = request.getParameter("sampleDate");
		String detectionDate = request.getParameter("detectionDate");
		String temerature = request.getParameter("temerature");
		String humidity = request.getParameter("humidity");;
		String detectionName = request.getParameter("detectionName");
		String method = request.getParameter("method");
		String standards = request.getParameter("standards");
		String comments = request.getParameter("comments");
		String detectorName = request.getParameter("detectorName");
		String detector = request.getParameter("detector");
		
		String[] items_id = request.getParameterValues("item_id");
		String[] items_sampleId = request.getParameterValues("sampleId");
		String[] items_deprecated = request.getParameterValues("deprecated");
		String[] items_grade1 = request.getParameterValues("grade1");
		String[] items_strength1 = request.getParameterValues("strength1");
		String[] items_grade2 = request.getParameterValues("grade2");
		String[] items_strength2 = request.getParameterValues("strength2");

		
		if (StringUtils.isBlank(detectionDate) || StringUtils.isBlank(id)) {
			log.info("Parameter String[detectionDate|ID] is blank.");
			return MessageModel.createErrorMessage("参数不足: 检测日期|ID必须提供", null);
		}
		SmellandTasteEntity entity = this.service.get(id);
		if (entity == null) {
			log.info("Can't found the task information which ID is '" + id + "'");
			return MessageModel.createErrorMessage("未找到与ID对应的检测任务", id);
		} else {
			try {
				entity.setSampleDate(Constants.formater.parse(sampleDate));
				entity.setDetectionDate(Constants.formater.parse(detectionDate));
			} catch (ParseException e) {
				log.info("Date format error:" + detectionDate);
				return MessageModel.createErrorMessage("时间格式错误", id);
			}
			List<SmellandTasteItemEntity> items = new ArrayList<SmellandTasteItemEntity>();
			for (int i=0 ; i < items_id.length; i++) {
				SmellandTasteItemEntity item = new SmellandTasteItemEntity();
				if (!StringUtils.isBlank(items_id[i])) {
					item.setId(items_id[i] );
				}
				item.setSampleId(items_sampleId[i]);
				item.setDeprecated(Integer.valueOf(items_deprecated[i]));
				item.setSmellAndTaste(entity);
				if (!StringUtils.isBlank(items_grade1[i])) {
					item.setGrade1(Double.valueOf(items_grade1[i]));
				}
				if (!StringUtils.isBlank(items_strength1[i])) {
					item.setStrength1(Double.valueOf(items_strength1[i]));
				}
				if (!StringUtils.isBlank(items_grade2[i])) {
					item.setGrade2(Double.valueOf(items_grade2[i]));
				}
				if (!StringUtils.isBlank(items_strength2[i])) {
					item.setGrade2(Double.valueOf(items_strength2[i]));
				}
				items.add(item);
			}
			entity.setTemerature(Double.valueOf(temerature));
			entity.setHumidity(Double.valueOf(humidity));
			entity.setDetectionName(detectionName);
			entity.setMethod(method);
			entity.setStandards(standards);
			entity.setComments(comments);
			entity.setDetectorName(detectorName);
			entity.setDetector(detector);
			entity.setItems(items);
			this.service.update(entity);
			return MessageModel.createSuccessMessage(null, null);
		}
		
	}
	
}
